{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 美股没有涨跌停，全靠市场自己调节。这样的话公司市值及流通性对涨跌幅有着至关重要的意义。\n",
    "# 需求，鉴定TSLA的异常阀值是多少；Top 10%被认为是异常值\n",
    "\n",
    "# 展示阀值\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['open', 'high', 'low', 'close', 'volume', 'pre_close', 'p_change',\n",
       "       'date', 'date_week', 'atr21', 'atr14', 'key'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 读取数据\n",
    "tsla_df = pd.read_csv('./tsla_2.csv',parse_dates=True, index_col=0)\n",
    "tsla_df.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x117139358>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXAAAAD4CAYAAAD1jb0+AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjAsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+17YcXAAAOyElEQVR4nO3dX4hc533G8eepkxajdRUbOYOqulm3OKGul6rV4hZMwyxpEtW+kB1qsClGpi7rgF0C0UXV3MS0BESp4ovSfzIW1kXsxRAbC9skdYU2IhBod1PRlSqMHUd1JQsJY0fxGuGy8q8Xe7Qdj2d3zvzbc36r7weGOec9Z2Z/7+zMo7Nn3vPKESEAQD6/UHUBAID+EOAAkBQBDgBJEeAAkBQBDgBJfWI9f9iWLVtifH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2/9q+0SH21pHSOck/VpE/I6kr0t62vYvj6rGbvrsQ6kpBqrQpT//KOk3JG3X8u9hf6XFllPb17pHd0TE72r5VNAjtj9fdUFXqXSfgZH9p8YR8Yd9POYDSR8Uy/O2fyLps5Iq+WKnnz6oxlMMlO2P7SckvTjicoahtq91LyLireL+gu3ntXxqqNP3Q3V33vbWiDhne6ukC1UX1IuIOH9lOctnoFanUGzfeOULP9u/LukWSW9UW1XPDku6z/Yv2b5Zy334t4pr6qr4wF1xj5a/pK279NM52N5k+7ory5K+pByvfSeHJe0ulndLeqHCWnqW8TMwsiPwtdi+R9LfSbpR0ku2j0fElyV9XtJf2V6SdFnSVyOill80rNaHiDhp+1lJ/yVpSdIjEXG5ylpL+hvb27V8CuK0pIerLae7IU3nULWGpOdtS8ufx6cj4nvVltSd7WckNSVtsX1G0jcl7ZP0rO2HJL0p6d7qKlzbKvU3s30GuJQeAJKq1SkUAEB5BDgAJEWAA0BSBDgAJEWAA0BSBDgAJEWAA0BS/wfRgRDDStmAwAAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 绘制变化率绘制直方图\n",
    "tsla_df.p_change.hist(bins=80)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4.894, 17.347]    32\n",
       "(3.34, 4.894]      32\n",
       "(2.752, 3.34]      32\n",
       "(2.19, 2.752]      32\n",
       "(1.244, 1.685]     32\n",
       "(0.834, 1.244]     32\n",
       "(0.495, 0.834]     32\n",
       "(0.259, 0.495]     32\n",
       "(-0.001, 0.259]    32\n",
       "(1.685, 2.19]      31\n",
       "Name: p_change, dtype: int64"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# qcut()函数进行涨跌幅平均分类 value_counts()函数经常和qcut()函数一起使用，便于更直观的显示分离结果\n",
    "# value_counts() 的使用需要记住，只有Series对象才有value_counts()方法 这里面可以找到10% Top的数据\n",
    "# 从数据中可以看到 4.894 是我们找到的对应的异常阀值\n",
    "import numpy as np\n",
    "cats = pd.qcut(np.abs(tsla_df.p_change),10)\n",
    "cats.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(-3.0, 0.0]     125\n",
       "(0.0, 3.0]      110\n",
       "(-5.0, -3.0]     29\n",
       "(3.0, 5.0]       25\n",
       "(7.0, inf]        9\n",
       "(-inf, -7.0]      8\n",
       "(5.0, 7.0]        7\n",
       "(-7.0, -5.0]      6\n",
       "Name: p_change, dtype: int64"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# pd.qcut() 将数据平均分为若干份，可以用pb.cut()传入bins .进行手工分类\n",
    "bins = [-np.inf, -7.0, -5, -3, 0, 3, 5, 7, np.inf]\n",
    "cats = pd.cut(tsla_df.p_change, bins)\n",
    "cats.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>cr_dummies_(-inf, -7.0]</th>\n",
       "      <th>cr_dummies_(-7.0, -5.0]</th>\n",
       "      <th>cr_dummies_(-5.0, -3.0]</th>\n",
       "      <th>cr_dummies_(-3.0, 0.0]</th>\n",
       "      <th>cr_dummies_(0.0, 3.0]</th>\n",
       "      <th>cr_dummies_(3.0, 5.0]</th>\n",
       "      <th>cr_dummies_(5.0, 7.0]</th>\n",
       "      <th>cr_dummies_(7.0, inf]</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-12-24</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-12-26</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-12-27</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-12-28</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-12-31</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            cr_dummies_(-inf, -7.0]  cr_dummies_(-7.0, -5.0]  \\\n",
       "2018-12-24                        1                        0   \n",
       "2018-12-26                        0                        0   \n",
       "2018-12-27                        0                        0   \n",
       "2018-12-28                        0                        0   \n",
       "2018-12-31                        0                        0   \n",
       "\n",
       "            cr_dummies_(-5.0, -3.0]  cr_dummies_(-3.0, 0.0]  \\\n",
       "2018-12-24                        0                       0   \n",
       "2018-12-26                        0                       0   \n",
       "2018-12-27                        0                       1   \n",
       "2018-12-28                        0                       1   \n",
       "2018-12-31                        0                       1   \n",
       "\n",
       "            cr_dummies_(0.0, 3.0]  cr_dummies_(3.0, 5.0]  \\\n",
       "2018-12-24                      0                      0   \n",
       "2018-12-26                      0                      0   \n",
       "2018-12-27                      0                      0   \n",
       "2018-12-28                      0                      0   \n",
       "2018-12-31                      0                      0   \n",
       "\n",
       "            cr_dummies_(5.0, 7.0]  cr_dummies_(7.0, inf]  \n",
       "2018-12-24                      0                      0  \n",
       "2018-12-26                      0                      1  \n",
       "2018-12-27                      0                      0  \n",
       "2018-12-28                      0                      0  \n",
       "2018-12-31                      0                      0  "
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# pb.cut() 函数经常会和pd.get_dummies()函数配合使用，将数据由连续数值变成离散类型，即数据的离散化，\n",
    "# get_dummies()生成离散化的哑变量矩阵多用于机器学习中监督学习问题的分类，使用它来作为训练数据使用\n",
    "\n",
    "# cr_dummies 为列名称前缀\n",
    "change_ration_dummies = pd.get_dummies(cats, prefix='cr_dummies')\n",
    "change_ration_dummies.tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "      <th>volume</th>\n",
       "      <th>pre_close</th>\n",
       "      <th>p_change</th>\n",
       "      <th>date</th>\n",
       "      <th>date_week</th>\n",
       "      <th>atr21</th>\n",
       "      <th>atr14</th>\n",
       "      <th>key</th>\n",
       "      <th>cr_dummies_(-inf, -7.0]</th>\n",
       "      <th>cr_dummies_(-7.0, -5.0]</th>\n",
       "      <th>cr_dummies_(-5.0, -3.0]</th>\n",
       "      <th>cr_dummies_(-3.0, 0.0]</th>\n",
       "      <th>cr_dummies_(0.0, 3.0]</th>\n",
       "      <th>cr_dummies_(3.0, 5.0]</th>\n",
       "      <th>cr_dummies_(5.0, 7.0]</th>\n",
       "      <th>cr_dummies_(7.0, inf]</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-12-24</th>\n",
       "      <td>313.5</td>\n",
       "      <td>314.50</td>\n",
       "      <td>295.195</td>\n",
       "      <td>295.39</td>\n",
       "      <td>5559913</td>\n",
       "      <td>319.77</td>\n",
       "      <td>-7.624</td>\n",
       "      <td>20181224</td>\n",
       "      <td>0</td>\n",
       "      <td>18.793476</td>\n",
       "      <td>19.634311</td>\n",
       "      <td>314</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-12-26</th>\n",
       "      <td>300.0</td>\n",
       "      <td>326.97</td>\n",
       "      <td>294.090</td>\n",
       "      <td>326.09</td>\n",
       "      <td>8163138</td>\n",
       "      <td>295.39</td>\n",
       "      <td>10.393</td>\n",
       "      <td>20181226</td>\n",
       "      <td>2</td>\n",
       "      <td>20.074069</td>\n",
       "      <td>21.400403</td>\n",
       "      <td>315</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-12-27</th>\n",
       "      <td>300.0</td>\n",
       "      <td>326.97</td>\n",
       "      <td>294.090</td>\n",
       "      <td>326.09</td>\n",
       "      <td>8163138</td>\n",
       "      <td>326.09</td>\n",
       "      <td>0.000</td>\n",
       "      <td>20181227</td>\n",
       "      <td>3</td>\n",
       "      <td>21.238245</td>\n",
       "      <td>22.931016</td>\n",
       "      <td>316</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-12-28</th>\n",
       "      <td>300.0</td>\n",
       "      <td>326.97</td>\n",
       "      <td>294.090</td>\n",
       "      <td>326.09</td>\n",
       "      <td>8163138</td>\n",
       "      <td>326.09</td>\n",
       "      <td>0.000</td>\n",
       "      <td>20181228</td>\n",
       "      <td>4</td>\n",
       "      <td>22.296586</td>\n",
       "      <td>24.257547</td>\n",
       "      <td>317</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-12-31</th>\n",
       "      <td>300.0</td>\n",
       "      <td>326.97</td>\n",
       "      <td>294.090</td>\n",
       "      <td>326.09</td>\n",
       "      <td>8163138</td>\n",
       "      <td>326.09</td>\n",
       "      <td>0.000</td>\n",
       "      <td>20181231</td>\n",
       "      <td>0</td>\n",
       "      <td>23.258715</td>\n",
       "      <td>25.407207</td>\n",
       "      <td>318</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             open    high      low   close   volume  pre_close  p_change  \\\n",
       "2018-12-24  313.5  314.50  295.195  295.39  5559913     319.77    -7.624   \n",
       "2018-12-26  300.0  326.97  294.090  326.09  8163138     295.39    10.393   \n",
       "2018-12-27  300.0  326.97  294.090  326.09  8163138     326.09     0.000   \n",
       "2018-12-28  300.0  326.97  294.090  326.09  8163138     326.09     0.000   \n",
       "2018-12-31  300.0  326.97  294.090  326.09  8163138     326.09     0.000   \n",
       "\n",
       "                date  date_week      atr21      atr14  key  \\\n",
       "2018-12-24  20181224          0  18.793476  19.634311  314   \n",
       "2018-12-26  20181226          2  20.074069  21.400403  315   \n",
       "2018-12-27  20181227          3  21.238245  22.931016  316   \n",
       "2018-12-28  20181228          4  22.296586  24.257547  317   \n",
       "2018-12-31  20181231          0  23.258715  25.407207  318   \n",
       "\n",
       "            cr_dummies_(-inf, -7.0]  cr_dummies_(-7.0, -5.0]  \\\n",
       "2018-12-24                        1                        0   \n",
       "2018-12-26                        0                        0   \n",
       "2018-12-27                        0                        0   \n",
       "2018-12-28                        0                        0   \n",
       "2018-12-31                        0                        0   \n",
       "\n",
       "            cr_dummies_(-5.0, -3.0]  cr_dummies_(-3.0, 0.0]  \\\n",
       "2018-12-24                        0                       0   \n",
       "2018-12-26                        0                       0   \n",
       "2018-12-27                        0                       1   \n",
       "2018-12-28                        0                       1   \n",
       "2018-12-31                        0                       1   \n",
       "\n",
       "            cr_dummies_(0.0, 3.0]  cr_dummies_(3.0, 5.0]  \\\n",
       "2018-12-24                      0                      0   \n",
       "2018-12-26                      0                      0   \n",
       "2018-12-27                      0                      0   \n",
       "2018-12-28                      0                      0   \n",
       "2018-12-31                      0                      0   \n",
       "\n",
       "            cr_dummies_(5.0, 7.0]  cr_dummies_(7.0, inf]  \n",
       "2018-12-24                      0                      0  \n",
       "2018-12-26                      0                      1  \n",
       "2018-12-27                      0                      0  \n",
       "2018-12-28                      0                      0  \n",
       "2018-12-31                      0                      0  "
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#  数据处理\n",
    "# concat() 合并  axis = 1 纵向上的连接数据， 如果横轴上连接数据， axis = 0, 更简单的方法 append()函数\n",
    "pd.concat([tsla_df, change_ration_dummies], axis=1).tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
